package tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import entities.Doc;
//class usage: 1. initial the class with the location where all docs are
//			   2. then pass the docid every time you want to retrive a doc

public class DocRetriever {
	
	//the Root location of all docs
	private String docRoot;
	
	public DocRetriever(){
		this.docRoot = "/largedata1/cmput696/TAC_2010_KBP_Source_Data/data/";
	}
	public DocRetriever(String docRoot){
		this.docRoot = docRoot;
	}
	
	public String getDocRoot() {
		return docRoot;
	}
	public void setDocRoot(String docRoot) {
		this.docRoot = docRoot;
	}
	//docid is part of the filename 
	public Doc getDocById( String docid) throws Exception{
		
		String path = getPath(docid);
		if ( path == null ){
			
			Exception e = new Exception("no such doc file");
			throw e;
		
		}else{
			
			String text = getFileContents(path);
			Doc doc = new Doc(docid,text);
			return doc;
		
		}
	
		
	}
	private  String getPath(String docid) {
		String docLoc = this.docRoot;
		String prefix1 = new String(docLoc + "2009/nw");
		String prefix2 = new String(docLoc + "2010/wb");
		String prefix3 = new String(docLoc + "2009/wb");
		
		String path = null;
		if (docid.startsWith("eng")) {
			path = prefix2 + "/" + docid + ".sgm";
		} else if (docid.contains(".com_")) {
			path = prefix3 + "/" + docid + ".sgm";
		} else if (docid.startsWith("cmn")) {
			path = null;
		} else if (docid.contains("_CMN_20")) {
			path = null;
		} else {
			String toks1[] = docid.toLowerCase().split("_");
			String toks2[] = toks1[2].split("\\.");
		
			path = prefix1 + "/" + toks1[0] + "_" + toks1[1] + "/" + toks2[0] + "/" + docid + ".sgm";
		}
		
		//#TEST  test the path
		System.out.println(path);
		
		if (path == null)
			return null;
		
		File f = new File(path);
		if (f.exists())
			return path;
		
		return null;
	}
	
	//for getting single document not located in the doc collection
	public static Doc getDocByAbsPath(String path) throws Exception{
		
		File f = new File(path);
		if( f.exists() ){
			String text = getFileContents(path);
			Doc doc = new Doc("noid",text);
			return doc;
		}else{
			Exception e = new Exception("no such file");
			throw e;
		}
	}
	
	//read the whole file as string, from internet
	public static String getFileContents(String fileName) throws Exception {
	    File theFile = new File(fileName);
	    byte[] bytes = new byte[(int) theFile.length()];
	    InputStream in = new FileInputStream(theFile);
	    int m = 0, n = 0;
	    while (m < bytes.length) {
		n = in.read(bytes, m, bytes.length - m);
		m += n;
	    }
	    in.close();
	    
	    // using default encoding, this is probably what BufferedReader.readLine does anyway
	    return new String(bytes); 
	}
	
	//TEST fucntion
	/*
	public static void main( String[] args ){
		DocRetriever dr = new DocRetriever();
		
		//TEST test getDocById
		
		String docid = "eng-WL-110-174612-12992627";
		try {
			Doc doc = dr.getDocById(docid);
			System.out.println("DOCID:"+doc.getDocid());
			System.out.println("**********************");
			System.out.println(doc.getText());
			System.out.println("***********************");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
	*/

	
}
